package com.java.controller;

import com.java.entity.Collec;
import com.java.entity.Malfunctions;
import com.java.entity.Orders;
import com.java.entity.User;
import com.java.service.UserService;
import com.java.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin  //允许跨域
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    public Map<String,Object> login(@RequestBody User user){ //@RequestBody   json ---> object
        user = userService.login(user.getUserName(), user.getUserPassword());
        Map<String,Object> map = new HashMap<>();
        if(user != null && user.getPermission()==true){
            map.put("statusCode",200);
            map.put("statusMessage","登录成功");
            map.put("data",user);
            return map;
        }else{
            map.put("statusCode",0);
            map.put("statusMessage","登录失败,用户名或密码不正确，或者该账号已被停用，请联系管理员");
            map.put("data","");
            return map;
        }
    }


    /**
     * 注册
     */
       @PutMapping("/register")
    public Map<String,Object> register(@RequestBody User user){
           Map<String,Object> map=new HashMap<>();
            User user1 = userService.registerPre(user.getUserIdcard());
            //User user2 = userService.login(user.getUserName(), user.getUserPassword());
            User user3 = userService.registerPree(user.getUserName());
           if (user3!=null){
                map.put("statusCode",0);
                map.put("statusMessage","注册失败，该账号已被注册");
                map.put("data","");
                return map;
            }
           if(!(user1 != null)){
               Boolean flag1 = userService.register(user);
                if (flag1){
                    map.put("statusCode",200);
                    map.put("statusMessage","注册成功");
                    map.put("data","");
                    return  map;
                }else {
                    map.put("statusCode",0);
                    map.put("statusMessage","注册失败，请重新填写注册信息");
                    map.put("data","");
                    return map;
                }
           }else {
               map.put("statusCode",0);
               map.put("statusMessage","注册失败，该身份证或账号已被注册，请换一个身份证号或更改账号");
               map.put("data","");
               return map;
           }


       }

    /**
     * 忘记密码进行修改
     * @param user
     * @return
     */
       @PutMapping("/forgetPwd")
       public Map<String,Object> forgetPwd(@RequestBody User user){
        Map<String,Object>map=new HashMap<>();
        Boolean flag = userService.forgetPwd(user.getUserName(), user.getUserIdcard(), user.getUserPassword());
        if (flag){
            map.put("statusCode",200);
            map.put("statusMessage","修改密码成功");
            map.put("data","");
        }else {
            map.put("statusCode",0);
            map.put("statusMessage","修改密码失败，请检查用户名或身份证号码是否正确");
            map.put("data","");
        }
           return map;
       }

    /**
     * 故障报修
     * @param malfunctions
     * @return
     */
    @PutMapping("/CallforRepairs")
    public Map<String,Object> CallforRepairs(@RequestBody Malfunctions malfunctions){

        Map<String,Object> map=new HashMap<>();

        if (userService.CallForRepairs(malfunctions)){
            map.put("statusCode",200);
            map.put("statusMessage","报修成功");
            System.out.println("66666");
            map.put("data","");
        }else {
            map.put("statusCode",0);
            map.put("statusMessage","报修失败");
            map.put("data","");
        }
        return map;
    }

    /**
     * 查看收藏
     * @param userId
     * @return
     */
    @GetMapping("/FindAllCollecsById")
    public Map<String,Object> FindAllCollecsById(@RequestParam("userId") String  userId){
        Map<String,Object> map=new HashMap<>();
        int i = Integer.parseInt(userId);
        List<Collec> collecs = userService.FindAllCollecsById(i);
        System.out.println(collecs);
        if (collecs != null){
            map.put("statusCode",200);
            map.put("statusMessage","查询收藏成功");
            map.put("data",collecs);
        }else {
            map.put("statusCode",0);
            map.put("statusMessage","查询失败或收藏为空");
            map.put("data","");
        }
        return map;
    }

    /**
     * 删除收藏
     * @param collec
     * @return
     */
    @PostMapping("/DeleteCollecById")
    public Map<String,Object> DeleteCollecById(@RequestBody Collec collec){
        Map<String,Object> map=new HashMap<>();
        if(userService.DeleteCollecById(collec.getUserId(),collec.getHouseId())){
            map.put("statusCode",200);
            map.put("statusMessage","删除成功");
            map.put("data","");
        }else {
            map.put("statusCode",0);
            map.put("statusMessage","删除失败");
            map.put("data","");
        }
        return map;
    }

    @GetMapping("FindUserByName")
    public Map<String,Object> FindUserByName(
            @RequestParam("userName") String userName,
            @RequestParam("userPassword") String userPassword){
        Map<String,Object> map=new HashMap<>();
        User user = new User();
        user.setUserName(userName);
        user.setUserPassword(userPassword);
        List<User> users = userService.FindUserByName(user);
        System.out.println(users);
        if(users != null){
            map.put("statusCode",200);
            map.put("statusMessage","查询成功");
            map.put("data",users);
        }else {
            map.put("statusCode",0);
            map.put("statusMessage","查询失败");
            map.put("data","");
        }
        return map;
    }

    /**
     * 修改个人信息
     * @param user
     * @return
     */
    @PutMapping("UpdateUserById")
    public Map<String,Object> UpdateUserById(@RequestBody User user){
        Map<String,Object> map=new HashMap<>();
        if(userService.UpdateUserById(user)){
            map.put("statusCode",200);
            map.put("statusMessage","修改成功");
            map.put("data","");
        }else {
            map.put("statusCode",0);
            map.put("statusMessage","修改失败");
            map.put("data","");
        }
        return map;
    }

    @GetMapping("FindOrdersById")
    public Map<String,Object> FindOrdersById(@RequestParam("userId") String  userId){
        Map<String,Object> map=new HashMap<>();
        List<Orders> orders = userService.findOrdersById(userId);
        System.out.println(orders);
        if (orders != null){
            map.put("statusCode",200);
            map.put("statusMessage","查询订单");
            map.put("data",orders);
        }else {
            map.put("statusCode",0);
            map.put("statusMessage","查询失败或订单为空");
            map.put("data","");
        }
        return map;
    }

}
